package com.example.sysao.controller;
//master
import com.example.sysao.pejo.Door;
import com.example.sysao.pejo.Record;
import com.example.sysao.pejo.Student;
import com.example.sysao.service.DoorService;
import com.example.sysao.service.RecordService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Component
@RequestMapping("/door")
public class DoorController {
    @Resource
    private DoorService doorService;
    @Resource
    private RecordService recordService;

    @RequestMapping("/list")
    public List<Door> findAll(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        return doorService.findByName(student.getDoor());
    }

    //根据door.id查询door
    @RequestMapping("/findbyname")
    Door findByname(String name) {
        return doorService.findByname(name);
    }

    //更新水电 增加费用
    @PostMapping("/saveDoor")
    public Map<String, Object> saveuser(@RequestBody Door door) {
        Map<String, Object> map = new HashMap<>();

        System.out.println(door);

        double power = door.getKilowatt() * 2;
        double water = door.getTon() * 3;

        Door door1 = door;
        door1.setWater(water);
        door1.setPower(power);


        if (doorService.saveDoor(door1)) {
            map.put("success", true);
        }
        return map;
    }

    @RequestMapping("/alldoors")
    public List<Door> alldoors(HttpSession session) {

        return doorService.findAllDoors();
    }

    @RequestMapping("/findDoor")
    public Door findStudent(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        Door door = findByname(student.getDoor());
        return door;
    }

    @PostMapping("/payment")
    public Map<String, Object> addReminder(@RequestBody Door door, HttpSession session) {
        // Student student = (Student) session.getAttribute("student");
        LocalDateTime time = LocalDateTime.now();
//        door.setName(student.getDoor());
//        door.setDate(time);
        //水电清零
        door.setKilowatt(0);
        door.setTon(0);
        
        door.setPower(0);
        door.setWater(0);
        //更新doord的水电
        doorService.saveDoor(door);
        //添加缴费记录
        Record record = new Record();
        record.setDoorname(door.getName());
        record.setPower(door.getPower());
        record.setWater(door.getWater());
        record.setSubmittime(time);

        Map<String, Object> map = new HashMap<>();
        if (recordService.addRecord(record)) {
            map.put("success", true);
        }
        return map;
    }
}
